package br.com.yurikimo.ykkit;

import java.util.Vector;

import android.os.CountDownTimer;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;

public abstract class YKScreenView extends RelativeLayout implements YKContainer, YKBaseComponent{
	private static final int ANIMATION_OPEN = 1;
	private static final int ANIMATION_CLOSE = 2;
	private static final int REMOVING = 3;
	private int animationType;
	private YKLayoutManager layoutManager;
	private RelativeLayout contentLayout = null;
	private float lastTop = 0;
	private boolean lockedScreen = false;
	private YKScreenView objectParent;
	private int openAnimationTime = -1;

	public static final int TRANSITION_SLIDE = 1;
	public static final int TRANSITION_FADE = 2;

	private int transitionType = TRANSITION_FADE;
	
	//private static int TRANSITION_TIME = 600;
	
	private Vector<Object> childreanList = new Vector<Object>();
	
	public void setTransitionType(int transitionType){
		this.transitionType = transitionType;
	}
	
	public YKScreenView(){
		super(YKDirector.getActivity());
		
		init(false);
	}
	
	public YKScreenView(boolean sync){
		super(YKDirector.getActivity());
		
		init(sync);
	}
	
	public abstract void onCreateScreen();
	
	public boolean isLocked(){
		return lockedScreen;
	}
	
	public YKScreenView(float x, float y, float width, float height){
		super(YKDirector.getActivity());
		
		init(false);
		
		setBounds(x, y, width, height);		
	}
	
	private void init(boolean sync){
		lockedScreen = true;
		
		layoutManager = new YKLayoutManager(this);
		
		contentLayout = new RelativeLayout(YKDirector.getActivity());
		contentLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
		
		addView(contentLayout);
		
		if (!sync){
			CountDownTimer countDownTimer = new CountDownTimer(50, 50)
			{
				
				@Override
				public void onTick(long millisUntilFinished)
				{
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onFinish()
				{
					onCreateScreen();
									
					if (openAnimationTime > 0){
						startOpenAniation(openAnimationTime);
						
						openAnimationTime = -1;
					}
				}
			};
			
			countDownTimer.start();
		}
		else{
			onCreateScreen();
			
			lockedScreen = false;
		}
	}
	
	public void removeChild(Object child){
		((YKScreenView)child).removeFromSuperView(false, 0);
	}
	
	public void removeAllChildrean(){
		for (int i = 0; i < childreanList.size(); i++){
				((YKBaseComponent)childreanList.elementAt(i)).removeFromSuperView();
		}

		childreanList.removeAllElements();
		
		lastTop = 0;
	}
	
	public void addChild(YKScreenView child, boolean animated, int animationTime){
		child.setPosition(child.getObjectX(), child.getObjectY());
		child.setObjectParent(this);
		child.addToView(this, animated, animationTime);
		
		childreanList.add(child);
	}

	public void addChild(YKBaseComponent child){
		childreanList.add(child);
		child.setObjectParent(this);
		child.setPosition(child.getObjectX(), child.getObjectY());
		
		child.addToLayout(contentLayout);
	}
	
	public void addChildAsList(YKBaseComponent child){
		childreanList.add(child);
		child.setObjectParent(this);
		child.setPosition(child.getObjectX(), lastTop);
		
		lastTop = child.getObjectY() + child.getObjectHeight();
		
		child.addToLayout(contentLayout);
		childreanList.add(child);
	}
	
	public void setBounds(float x, float y, float width, float height){
		layoutManager.setBounds(x, y, width, height);
	}

	private void startOpenAniation(int animationTime){
	  animationType = ANIMATION_OPEN;
	
	  if (transitionType == TRANSITION_FADE){
		  Animation animation = new AlphaAnimation(0.0f, 1.0f);
		  animation.setDuration(animationTime);
		  startAnimation(animation);
	  }
	  else if (transitionType == TRANSITION_SLIDE)
	  {
		  AnimationSet animationSet = new AnimationSet(false);
		  
		  Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		  animation.setDuration(animationTime);
		  
		  animationSet.addAnimation(animation);
		  startAnimation(animation);
	  }
	}
	
	private void startCloseAniation(int animationTime){
	  animationType = ANIMATION_CLOSE;

	  if (transitionType == TRANSITION_FADE){
		  Animation animation = new AlphaAnimation(1.0f, 0.01f);
		  animation.setDuration(animationTime);
		  startAnimation(animation);
	  }
	  else if (transitionType == TRANSITION_SLIDE)
	  {
		  Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);	  
		  animation.setDuration(animationTime);
		  animation.setFillAfter(true);
		  startAnimation(animation);
	  }
	}
	
	protected void addToView(RelativeLayout relativeLayout, boolean animated, int animationTime){		
		layoutManager.addToLayout(relativeLayout);
		
		if (!lockedScreen){
			if (animated){
				startOpenAniation(animationTime);
			}
		}
		else if (animated){
			openAnimationTime = animationTime;
		}
	}
	
	public void addToView(RelativeLayout relativeLayout){
		addToView(relativeLayout, false, 0);
	}
	
	public void refreshLayout(){
		layoutManager.refreshLayout();
	}
	
	public void setPosition(float x, float y){
		layoutManager.setPosition(x, y);
	}
	
	public float getObjectWidth(){
		return layoutManager.getObjectWidth();
	}
	
	public float getObjectHeight(){
		return layoutManager.getObjectHeight();
	}
	
	public float getObjectX(){
		return layoutManager.getObjectX();
	}
	
	public float getObjectY(){
		return layoutManager.getObjectY();
	}

	public boolean removeFromSuperView(boolean animated, int animationTime){
		YKScreenView screenView = null;
		
		for (int i = childreanList.size() - 1; i >= 0; i--){
			Object object = childreanList.elementAt(i);
			
			if (object instanceof YKScreenView){
				screenView = ((YKScreenView)childreanList.elementAt(i));
				
				break;
			}
		}

		if (screenView == null){
			if (lockedScreen){
				return true;
			}
			
			if (animated){
				startCloseAniation(animationTime);
			}
			else{
				layoutManager.getLayoutView(getContext()).removeView(this);
				
				((YKScreenView)getObjectParent()).onRemoveChild(this);
			}
			
			return false;			
		}
		else{
			if (!screenView.removeFromSuperView(animated, animationTime)){
				childreanList.remove(screenView);
			}
			
			return true;
		}
	}
	
	public void uncheckButtons(YKButton button){
		for (int i = 0; i < childreanList.size(); i++){
			if (childreanList.get(i) instanceof YKButton){
				YKButton btnTemp = (YKButton)childreanList.get(i);
				
				if (btnTemp.isCheckButton() && !btnTemp.equals(button)){
					btnTemp.chek(false);
				}
			}
		}
	}
	
	@Override
	protected void onAnimationEnd()
	{
		if (animationType == ANIMATION_CLOSE){
			animationType = REMOVING;
			
		  Animation animation = new ScaleAnimation(0.01f, 0.01f, 0.01f, 0.01f);
 
		  animation.setDuration(0);
		  animation.setFillAfter(true);

		  startAnimation(animation);
		}
		else if (animationType == REMOVING){
			layoutManager.getLayoutView(getContext()).removeView(this);
			((YKScreenView)getObjectParent()).childreanList.remove(this);
			((YKScreenView)getObjectParent()).onRemoveChild(this);
		}
		else if (animationType == ANIMATION_OPEN){
			lockedScreen = false;
		}
	}
	
	@Override
	public void addToLayout(RelativeLayout relativeLayout) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public RelativeLayout getLayoutView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public YKContainer getObjectParent() {
		return objectParent;
	}

	@Override
	public void removeFromSuperView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setObjectParent(YKContainer parent) {
		objectParent = (YKScreenView)parent;
	}
	
	public abstract void onRemoveChild(YKScreenView screenView);
}
